package com.yiwei.hdb.api.requestInfo;

import android.content.Intent;
import android.text.TextUtils;

import com.yiwei.hdb.activity.CameraActivity;
import com.yiwei.hdb.api.Api;
import com.yiwei.hdb.api.bean.ChsiBean;
import com.yiwei.hdb.api.bean.ChsiLoginBean;
import com.yiwei.hdb.api.bean.IdCardIndentityBean;
import com.yiwei.hdb.api.bean.UploadSubmitIdcardBean;
import com.yiwei.hdb.api.callback.ChsiIdcardContract;
import com.yiwei.hdb.model.IdentityModel;
import com.yiwei.hdb.util.Network;
import com.yiwei.hdb.util.RxLifeUtil;
import com.yiwei.hdb.util.ToastUtil;

/**
 * Created by Administrator on 2017/5/14.
 */

public class ChsiIdcardRequest extends BaseRequest<ChsiIdcardContract.View> implements ChsiIdcardContract.Presenter<ChsiIdcardContract.View> {

    public static ChsiIdcardRequest create() {
        return new ChsiIdcardRequest();
    }


    public void startCameraActivity() {
        Intent localIntent = new Intent(BaseContext, CameraActivity.class);
        localIntent.putExtra("path", "/sdcard/pictures");
        localIntent.putExtra("name", "idcard.jpg");
        localIntent.putExtra("type", "idcard");
//        ((ChsiIdcardActivity) BaseContext).startActivityForResult(localIntent, 100);
    }

    @Override
    public void getIdentityInfo(int paramInt, String paramString) {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail);
            return;
        }
        mView.onRequestStart();
        IdCardIndentityBean bean = new IdCardIndentityBean();
        bean.img = paramString;
        Api.api_service.getIdentityInfo(paramInt, bean).compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    if (result.getStatus() != 0) {
                        mView.getIdentityInfoSuccess(paramInt, result);
                    }
                });
    }

    @Override
    public void getSubmitidCard(String name, String idcard, UploadSubmitIdcardBean item) {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail);
            return;
        }
        mView.onRequestStart();
        Api.api_service.getSubmitidCard(item).compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.getSubmitidCardSuccess(name, idcard, result);
                });
    }

    @Override
    public void getChsiCode(String username, String password) {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail);
            return;
        }
        mView.onRequestStart();
        ChsiBean bean = new ChsiBean();
        bean.username = username;
        bean.password = password;
        Api.api_service.getChsiCodeInfo(bean).compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.getChsiCodeSuccess(result);
                });
    }

    @Override
    public void getChsiLogin(String username, String password, String captcha) {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail);
            return;
        }
        mView.onRequestStart();
        ChsiLoginBean bean = new ChsiLoginBean();
        bean.username = username;
        bean.password = password;
        if (!TextUtils.isEmpty(captcha)) {
            bean.captcha = captcha;
        }
        Api.api_service.getChsiLoginInfo(bean).compose(RxLifeUtil.NocheckOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    if (result.getStatus() == 1) {
                        mView.getChsiLoginSuccess(result);
                    } else if (result.getStatus() == 2) {
                        mView.getChsiNeedCode(result.captchaImage);
                    } else {
                        ToastUtil.makeText(BaseContext, result.getMsg());
                    }
                });
    }
}
